<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtiframe.inc.php");
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("comctrls.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("components4phpfull/jtdatepicker.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class DriveCreate extends Page
{
       public $hfincident = null;
       public $btnAddG = null;
       public $Label14 = null;
       public $Image3 = null;
       public $JSValidate = null;
       public $btnAdd = null;
       public $edDesc = null;
       public $Label8 = null;
       public $JTGroupBox2 = null;
       public $Label9 = null;
       public $edAmt = null;
       public $hfVeh = null;
       public $JTIFrameCost = null;
       public $memoDesc = null;
       public $cmbdriver = null;
       public $Label6 = null;
       public $Label5 = null;
       public $edIncidentNum = null;
       public $imgBack = null;
       public $btnCancel = null;
       public $btnSubmit = null;
       public $btnAddType = null;
       public $btnDelType = null;
       public $edType = null;
       public $listType = null;
       public $JTDivType = null;
       public $btnShowType = null;
       public $JTGroupBox1 = null;
       public $compid = null;
       public $JTJavaScript3 = null;
       public $JTJavaScript2 = null;
       public $JTJavaScript1 = null;
       public $dpDate = null;
       public $hiddenEmpId = null;
       public $memoComments = null;
       public $edRefNum = null;
       public $edCompLiability = null;
       public $edPtAssessed = null;
       public $cmbType = null;
       public $Label4 = null;
       public $Label3 = null;
       public $Label2 = null;
       public $Label1 = null;
       public $chkpersonalinjury = null;
       public $chkatfault = null;
       public $lblMobile = null;
       public $lblJob = null;
       public $JTSiteTheme1 = null;
       public $lblBirthdate = null;
       public $Image1 = null;
       function btnAddGJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var veh = document.getElementById("hfVeh").value;
       var inc = document.getElementById("hfincident").value;
       var win_height = 120;
       var win_width = 304;
       var height = window.outerHeight;
       var width = window.outerWidth;
       var top = Math.round((height-win_height)/2);
       var left = Math.round((width-win_width)/2);
       window.open("cost_add.php?veh="+veh+"&inc="+inc,'cost_add','location=no,height='+win_height+',width='+win_width+',top='+top+',left='+left);
       <?php

       }

       function btnAddClick($sender, $params)
       {
       $result_cost = mysql_query("insert into incident_cost_tbl (company_id,
       vehicle_id,
       incident_id,
       description,
       amount)

       values ('".$_SESSION['company_id']."',
       '".$this->hfVeh->getValue()."',
       '".$_SESSION['rand']."',
       '".$this->edDesc->Text."',
       '".$this->edAmt->Text."')");

       echo "<script language=javascript>
       document.getElementById('JTIFrameCost').location.reload();
       </script>";

       return false;

       }

       function btnAddJSClick($sender, $params)
       {
       $this->add_cost();
       ?>
       //Add your javascript code here
       document.getElementById('edDesc').value='';
       document.getElementById('edAmt').value='';
       <?php

       }

       function add_cost($sender, $params)
       {
       $result_cost = mysql_query("insert into incident_cost_tbl (company_id,
       vehicle_id,
       incident_id,
       description,
       amount)

       values ('".$_SESSION['company_id']."',
       '".$this->hfVeh->getValue()."',
       '".$_SESSION['incident_id']."',
       '".$this->edDesc->Text."',
       '".$this->edAmt->Text."')");
       }


       function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back.png"
       <?php

       }

       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back2.png"
       <?php

       }

       function btnDelTypeJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var TypeIndex = document.getElementById('listType').selectedIndex;
       var TypeValue = document.getElementById('listType')[TypeIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxType3(TypeValue,comp);
       <?php

       }


       function btnAddTypeJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addtype = document.getElementById('edType').value;
       var comp = document.getElementById('compid').value;
       AjaxType2(addtype,comp);
       document.DriveCreate.edType.value = "";
       <?php

       }

       function btnShowTypeJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivType').Show();
       document.DriveCreate.edType.value = "";
       <?php

       }


       function btnCancelClick($sender, $params)
       {
       redirect('veh_incident_list.php?veh='.$this->hfVeh->getValue());


       }

       function clr_drive()
       {
       $this->dpDate->Text='';
       $this->edDesc->Text='';
       $this->edRefNum->Text='';
       $this->edPtAssessed->Text='';
       $this->edCompLiability->Text='';
       $this->memoComments->Text='&nbsp';


       $this->cmbType->Clear();
       $this->cmbType->AddItem('--Select--', null, null);
       $result_type = mysql_query("select value from lookup_personnel_tbl where company_id='".$_SESSION['company_id']."' AND category='driving history types'");

        while($row_type = mysql_fetch_array($result_type))
        {
            $this->cmbType->AddItem($row_type['value'], null, $row_type['value']);
        }
       }


       function generate_last_workorder()
       {
        $company_id = $_SESSION['company_id'];
        $result_incident = mysql_query("SELECT * FROM veh_incident_tbl WHERE company_id='".$company_id."'")
        or die("generate incident ".mysql_error());
        $num_incident = mysql_num_rows($result_incident);
        $row_incident = mysql_fetch_array($result_incident);

        //$result_last = mysql_query("SELECT * FROM veh_incident_tbl WHERE company_id='".$company_id."' order by incident_id DESC limit 1");
        //$row_last = mysql_fetch_array($result_last);

        $this->edIncidentNum->ReadOnly = true;
        $prefix = 'IR#0000';
        $last_num = $num_incident+1;
        $this->edIncidentNum->Text = $prefix.$last_num;

       }



       function DriveCreateCreate($sender, $params)
       {
       $this->compid->Value = $_SESSION['company_id'];
       $veh = $_GET['veh'];
       $this->hfVeh->setValue($veh);

       $this->generate_last_workorder();

       if(!isset($_SESSION['rand']))
       {
       $_SESSION['rand'] = rand() . "\n";
       }

       $this->hfincident->setValue($_SESSION['rand']);

       $this->JTIFrameCost->setURL('incident_cost_list.php?incident='.$_SESSION['rand']);
       $this->imgBack->setLink('veh_incident_list.php?layer=1&veh='.$this->hfVeh->getValue());

       /*$this->cmbveh->Clear();
       $this->cmbveh->AddItem('--Select--', null, null);
        $result_veh = mysql_query("select * from vehicle_tbl where company_id='1'");

        while($row_veh = mysql_fetch_array($result_veh))
        {
            $this->cmbveh->AddItem($row_veh['veh_num'], null, $row_veh['vehicle_id']);
        }*/

       $this->cmbdriver->Clear();
       $this->cmbdriver->AddItem('--Select--', null, null);
       $this->cmbdriver->setItemIndex('--Select--');
       $result_driver = mysql_query("select * from employee_tbl where company_id='".$_SESSION['company_id']."' and veh_op='1'");

       while($row_driver = mysql_fetch_array($result_driver))
        {
            $this->cmbdriver->AddItem($row_driver['lname'].','.$row_driver['fname'], null, $row_driver['lname'].','.$row_driver['fname']);
        }


       $this->cmbType->Clear();
       $this->cmbType->AddItem('--Select--', null, null);
       $this->cmbType->setItemIndex('--Select--');
       $result_type = mysql_query("select value from lookup_personnel_tbl where company_id='".$_SESSION['company_id']."' AND category='driving history types'");

        while($row_type = mysql_fetch_array($result_type))
        {
            $this->cmbType->AddItem($row_type['value'], null, $row_type['value']);
        }

        //list type
       $result_t = mysql_query("select value from lookup_personnel_tbl where company_id='".$_SESSION['company_id']."' AND category='driving history types'");

        while($row_t = mysql_fetch_array($result_t))
        {
            $this->listType->AddItem($row_t['value'], null, $row_t['value']);
        }

       $this->dpDate->Text= date('Y-m-d');
       $this->edDesc->Text='';
       $this->edRefNum->Text='';
       $this->edPtAssessed->Text='';
       $this->edCompLiability->Text='';
       $this->memoDesc->Lines=array();
       $this->edAmt->Text='';
       }

       function btnSubmitClick($sender, $params)
       {

       $company_id = $_SESSION['company_id'];
       $result_incident = mysql_query("SELECT * FROM veh_incident_tbl WHERE company_id='".$company_id."'")
       or die("generate incident ".mysql_error());
       $num_incident = mysql_num_rows($result_incident);
       $prefix = 'IR#0000';
       $incident_num = $num_incident+1;
       $save_incident = $prefix.$incident_num;
       // INSERT TO VEHICLE INCIDENT
        $result = mysql_query("insert into veh_incident_tbl (incident_num,
       company_id,
       vehicle_id,
       date,
       type,
       description,
       driver)

       values ('".$save_incident."',
       '".$_SESSION['company_id']."',
       '".$this->hfVeh->getValue()."',
       '".$this->dpDate->Text."',
       '".$this->cmbType->getItemIndex()."',
       '".$this->memoDesc->Text."',
       '".$this->cmbdriver->getItemIndex()."')");


       $result_incident_id = mysql_query("SELECT LAST_INSERT_ID(incident_id) AS incident_id FROM veh_incident_tbl ORDER BY incident_id DESC LIMIT 0,1") or die("SELECT INCIDENT ID - ".mysql_error());
       $row_incident_id = mysql_fetch_array($result_incident_id);
       $incident_id = $row_incident_id['incident_id'];

       $query_up_inc = "UPDATE incident_cost_tbl SET
       incident_id='".$incident_id."',
       date='".$this->dpDate->Text."',
       type='".$this->cmbType->getItemIndex()."',
       desc_main='".$this->memoDesc->Text."' WHERE incident_id='".$_SESSION['rand']."'";
       mysql_query($query_up_inc) or die("UPDATE INC ".mysql_error());



       //redirect('veh_incident_list.php?veh='.$this->hfVeh->getValue());
       echo "<script type='text/javascript'>
       var answer = confirm('Add another record?');
       if(answer)
       {
       window.location = 'veh_incident_create.php?veh=".$this->hfVeh->getValue()."';
       }
       else
       {
       window.location = 'veh_incident_list.php?veh=".$this->hfVeh->getValue()."';
       }
       </script>";

       }

}

global $application;

global $DriveCreate;

//Creates the form
$DriveCreate=new DriveCreate($application);

//Read from resource file
$DriveCreate->loadResource(__FILE__);

//Shows the form
$DriveCreate->show();

?>